/***************************************************************************
 *   Copyright (C) 2005 by xihe   *
 *   xihels@163.com                                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "Packet.h"
#include "QQClient.h"


/*
 * =====================================================================================
 *        Class:  OupPacket
 *  Description:  所有输出包的基类
 * =====================================================================================
 */
OutPacket::OutPacket(QQClient& cl,
	uint16_t    _command,
	int	    _sendCount) :
    Packet(QQ_CLIENT,
	    _command, 
	    cl.stepSequence(), 
	    QQ_HEADER_BASIC_FAMILY) ,
    client(cl),
    sendTime_(0),
    sendCount_(_sendCount)
{}

OutPacket::OutPacket(const QQClient& cl, const uint8_t* buf) :
    Packet(buf, QQ_HEADER_LENGTH_BASIC_FAMILY_OUT), 
    client(cl),
    sendTime_(0),
    sendCount_(1)
{
    source = QQ_CLIENT;
}

int OutPacket::putHead(uint8_t* buf) const {
    uint8_t* cur = buf;
    cur += writeBufB(cur, header);
    cur += writeBufW(cur, source);
    cur += writeBufW(cur, command);
    cur += writeBufW(cur, sequence);
    cur += writeBufDw(cur, client.getUid());

    return cur - buf;
}



/*
 * =====================================================================================
 *        Class:  ReplyPacket
 *  Description:  收到一些消息时,给服务器发的回复包
 *		  收到的消息的前16个字节(不包括消息头)
 * =====================================================================================
 */
ReplyPacket::ReplyPacket(const QQClient& cl, const uint8_t* buf) :
    OutPacket(cl, buf)
{
    // 注意, 这里是 QQ_HEADER_LENGTH_BASIC_FAMILY_IN,
    // 而不是 QQ_HEADER_LENGTH_BASIC_FAMILY_OUT.
    // 因为 buf是前面接收到的消息.
    writeBufData(ackBody, buf + QQ_HEADER_LENGTH_BASIC_FAMILY_IN, 16);
}


int ReplyPacket::putBody(uint8_t* buf) const
{
    return writeBufData(buf, ackBody, 16);
}



/*
 * =====================================================================================
 *        Class:  UnknowPacket
 *  Description:  收到无法处理的包时,解释成这个包
 * =====================================================================================
 */
UnknowPacket::UnknowPacket(const uint8_t* buf, int len) :
    InPacket(buf, len)
{
    unknow.assign(buf + QQ_HEADER_LENGTH_BASIC_FAMILY_IN, 
	    buf + len);
}

void UnknowPacket::process(QQClient& client)
{
    client.onUnknow(*this);
}



